package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.KfnProjectCostBo;
import com.ruoyi.system.domain.vo.KfnProjectCostVo;
import com.ruoyi.system.domain.KfnProjectCost;
import com.ruoyi.system.mapper.KfnProjectCostMapper;
import com.ruoyi.system.service.IKfnProjectCostService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目费用Service业务层处理
 *
 * @author Panda
 * @date 2023-12-21
 */
@RequiredArgsConstructor
@Service
public class KfnProjectCostServiceImpl implements IKfnProjectCostService {

    private final KfnProjectCostMapper baseMapper;

    /**
     * 查询项目费用
     */
    @Override
    public KfnProjectCostVo queryById(Long projectCostId){
        return baseMapper.selectVoById(projectCostId);
    }

    /**
     * 查询项目费用列表
     */
    @Override
    public TableDataInfo<KfnProjectCostVo> queryPageList(KfnProjectCostBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KfnProjectCost> lqw = buildQueryWrapper(bo);
        Page<KfnProjectCostVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目费用列表
     */
    @Override
    public List<KfnProjectCostVo> queryList(KfnProjectCostBo bo) {
        LambdaQueryWrapper<KfnProjectCost> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KfnProjectCost> buildQueryWrapper(KfnProjectCostBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KfnProjectCost> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getProjectCostType()), KfnProjectCost::getProjectCostType, bo.getProjectCostType());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectCostCategory()), KfnProjectCost::getProjectCostCategory, bo.getProjectCostCategory());
        lqw.like(StringUtils.isNotBlank(bo.getProjectCostFromUser()), KfnProjectCost::getProjectCostFromUser, bo.getProjectCostFromUser());
        lqw.between(params.get("beginProjectCostDate") != null && params.get("endProjectCostDate") != null,
            KfnProjectCost::getProjectCostDate ,params.get("beginProjectCostDate"), params.get("endProjectCostDate"));
        lqw.eq(bo.getProjectCostAmount() != null, KfnProjectCost::getProjectCostAmount, bo.getProjectCostAmount());
        lqw.like(StringUtils.isNotBlank(bo.getProjectCostToUser()), KfnProjectCost::getProjectCostToUser, bo.getProjectCostToUser());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), KfnProjectCost::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkOrderId()), KfnProjectCost::getWorkOrderId, bo.getWorkOrderId());
        return lqw;
    }

    /**
     * 新增项目费用
     */
    @Override
    public Boolean insertByBo(KfnProjectCostBo bo) {
        KfnProjectCost add = BeanUtil.toBean(bo, KfnProjectCost.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectCostId(add.getProjectCostId());
        }
        return flag;
    }

    /**
     * 修改项目费用
     */
    @Override
    public Boolean updateByBo(KfnProjectCostBo bo) {
        KfnProjectCost update = BeanUtil.toBean(bo, KfnProjectCost.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KfnProjectCost entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除项目费用
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
